﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace System.Utility.Messaging
{
    public class MessageFactory
    {
        public const string MessageNotFoundErrorText = "Unable to retrieve message from repository";

        static XmlNodeList messageList = null;

        public static string GetMessageByCode(string messageCode)
        {
            string message = MessageNotFoundErrorText;
            if (messageList == null)//lazy load or we can use caching for the same 
            {
                XmlDocument doc = Loader.LoadXmlDocument(Settings.GetSettingByKey("MessagePath"));
                messageList = doc.GetElementsByTagName("Message");
            }

            foreach (XmlNode node in messageList)
            {
                if (node.Attributes["Code"].Value != messageCode)
                    continue;
                else
                    message = node.Attributes["Text"].Value;
                break;
            }
            return message;

        }
    }
}
